package mytest.controller;


import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import mytest.util.RequestUtil;
import mytest.util.Response;
import test.service.ServiceUser;
import test.testcase.dao.ChannelDao;
import test.testcase.dao.projectDao;
import test.user.user;
import test.utils.JsonResult;

@Controller @RequestMapping(value = "/project", produces = "application/json; charset=utf-8")
@CrossOrigin
public class project {
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody String getlist(HttpServletRequest request) {
		String userid = RequestUtil.getUserId(request);
		return projectDao.getList(userid).toResponse();
	}
	
	@RequestMapping(value = "/sysparam/getlist", method = RequestMethod.GET)
	public @ResponseBody String getsysparamlist(HttpServletRequest request) {
		return projectDao.getParamList(RequestUtil.getStartPage(request), RequestUtil.getEndPage(request), RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody String add(@RequestBody String body, HttpServletRequest request) {
		JsonResult result = JsonResult.parse(body);
		return projectDao.add(result.read("projectname").toString(), result.read("preurl").toString(), RequestUtil.getUserId(request), result.read("type").toString()).toResponse();
	}
	
	@RequestMapping(value = "/del/{id}", method = RequestMethod.GET)
	public @ResponseBody String del(@PathVariable String id) {
		JsonResult result = projectDao.del(id);
		if (result == null) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("项目不能删除，因为有用例等依赖");
			return response.toString();
		}
		return projectDao.del(id).toResponse();
	}
	
	@RequestMapping(value = "/sysparam/del/{paramname}", method = RequestMethod.GET)
	public @ResponseBody String del(@PathVariable String paramname, HttpServletRequest request) {
		return projectDao.delsysparam(RequestUtil.getProjectId(request), paramname).toResponse();
	}
	
	@RequestMapping(value = "/info/{id}", method = RequestMethod.GET)
	public @ResponseBody String getinfo(@PathVariable String id) {
		return projectDao.getInfo(id).toResponse();
	}
	
	@RequestMapping(value = "/getsysparam/{paramname}", method = RequestMethod.GET)
	public @ResponseBody String getsysparaminfo(HttpServletRequest request, @PathVariable String paramname) {
		return projectDao.getSysparamInfo(paramname, RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/save/{id}", method = RequestMethod.POST)
	public @ResponseBody String save(@RequestBody String body, @PathVariable String id) {
		JsonResult result = JsonResult.parse(body);
		String projectname = result.read("projectname").toString();
		String preurl = result.read("preurl").toString();
		JSONArray array = ChannelDao.list().read("data").toJsonArray();
		ChannelDao.delProjectChannel(id);
		for(int i=0;i<array.length();i++) {
			JSONObject object = array.getJSONObject(i);
			String channel = object.getString("name");
			if (result.read(channel) != null && "on".equals(result.read(channel).toString())) {
				ChannelDao.add2toProject(channel, id);
			}
		}
		return projectDao.save(projectname, preurl, id).toResponse();
	}
	
	@RequestMapping(value = "/addsysparam", method = RequestMethod.POST)
	public @ResponseBody String addsysparam(@RequestBody String body, HttpServletRequest request) {
		JsonResult result = JsonResult.parse(body);
		String paramname = result.read("paramname").toString();
		String value = result.read("value").toString();
		String projectid = RequestUtil.getProjectId(request);
		String desc = result.read("desc").toString();
		if (projectid == null || projectid.equals("")) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("请选择项目");
			return response.toString();
		}
		JsonResult result2 = projectDao.addsysparam(paramname, value, projectid, desc);
		if (result2 == null) {
			Response response = new Response();
			response.setRetCodeFalse();
			response.setRetMessage("参数不能重复");
			return response.toString();
		}
		return result2.toResponse();
	}
	
	@RequestMapping(value = "/savesysparam", method = RequestMethod.POST)
	public @ResponseBody String savesysparam(@RequestBody String body, HttpServletRequest request) {
		JsonResult result = JsonResult.parse(body);
		String paramname = result.read("paramname").toString();
		String value = result.read("value").toString();
		String projectid = RequestUtil.getProjectId(request);
		String desc = result.read("desc").toString();
		JsonResult result2 = projectDao.savesysparam(paramname, value, projectid, desc);
		return result2.toResponse();
	}
	
	@RequestMapping(value = "/typetag/{id}", method = RequestMethod.GET)
	public @ResponseBody String typetaglist(@PathVariable String id) {
		return projectDao.typetaglist(id).toResponse();
	}
	
	@RequestMapping(value = "/addtypetag", method = RequestMethod.POST)
	public @ResponseBody String addtypetag(@RequestBody String body, HttpServletRequest request) {
		return projectDao.addtypetag(body, RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/typelist", method = RequestMethod.GET)
	public @ResponseBody String typelist(HttpServletRequest request) {
		return projectDao.typetaglist(RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/deltype/{id}", method = RequestMethod.GET)
	public @ResponseBody String deltype(@PathVariable String id) {
		return projectDao.deltype(id).toResponse();
	}
	
	@RequestMapping(value = "/typeinfo/{id}", method = RequestMethod.GET)
	public @ResponseBody String typeinfo(@PathVariable String id) {
		return projectDao.typeinfo(id).toResponse();
	}
	
	@RequestMapping(value = "/savetypetag/{id}", method = RequestMethod.POST)
	public @ResponseBody String savetypetag(@RequestBody String body, @PathVariable String id) {
		return projectDao.savetypetag(body, id).toResponse();
	}
	
	@RequestMapping(value = "/removeuser/{userid}", method = RequestMethod.GET)
	public @ResponseBody String removeuser(@PathVariable String userid, HttpServletRequest request) {
		user user = ServiceUser.getUser(userid);
		if (user.getUsername().equals("admin") || RequestUtil.getUserId(request).equals(userid)) {
			return new Response().setFalse("不能删除本人或管理员").toString();
		}
		return projectDao.deluserfromproject(RequestUtil.getProjectId(request), userid).toResponse();
	}
	
	@RequestMapping(value = "/adduser/{userid}", method = RequestMethod.GET)
	public @ResponseBody String adduser(@PathVariable String userid, HttpServletRequest request) {
		return projectDao.addusertoproject(RequestUtil.getProjectId(request), userid).toResponse();
	}
	
	@RequestMapping(value = "/tongji/fenlei", method = RequestMethod.GET)
	public @ResponseBody String fenleiTongji(HttpServletRequest request) {
		return projectDao.tongjiFenlei(RequestUtil.getProjectId(request)).toResponse();
	}
	
	@RequestMapping(value = "/tongji/excute", method = RequestMethod.GET)
	public @ResponseBody String excuteTongji(HttpServletRequest request) {
		return projectDao.tongjiExcuteResult(RequestUtil.getProjectId(request)).toResponse();
	}
	
}
